package com.yaolaizaixian.merchant.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.base.BusinessEmue;
import com.yaolaizaixian.merchant.bean.RightsDetailInfo;
import com.yaolaizaixian.merchant.utils.BusinessUtils;

import java.util.List;

/**
 * Created by ylzx on 2017/5/15.
 */
public class RightsAdapter extends RecyclerView.Adapter<RightsAdapter.ViewHolder> {

    private OnItemClickListener mOnItemClickListener;

    private List<RightsDetailInfo> merList;
    private Context mcontext;

    public RightsAdapter(Context context, List<RightsDetailInfo> erList) {

        this.merList = erList;
        this.mcontext = context;

    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_showquanyi, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RightsAdapter.ViewHolder holder, final int position) {

        holder.tv_rights_title.setText(merList.get(position).getName());
        holder.tv_rights_data.setText("期限："+merList.get(position).getEndDate());
        holder.tv_rights_huiyuan.setText(BusinessUtils.getCustomerRank(merList.get(position).getLevel()));
        holder.xiangqingtext.setText(Html.fromHtml("<u>详情</u>"));

        Glide
                .with(mcontext)
                .load(merList.get(position).getAppImage())
                .placeholder(R.mipmap.quanyi_zhanwei)
                .error(R.mipmap.quanyi_zhanwei)
                .into(holder.iv_rights_logo);

        switch (merList.get(position).getEquityType())
        {
            case BusinessEmue.Food:

                holder.iv_rights_lei.setImageDrawable(mcontext.getResources().getDrawable(R.mipmap.food));

                break;

            case BusinessEmue.Travel:

                holder.iv_rights_lei.setImageDrawable(mcontext.getResources().getDrawable(R.mipmap.airplane));

                break;

            case BusinessEmue.Entertainment:

                holder.iv_rights_lei.setImageDrawable(mcontext.getResources().getDrawable(R.mipmap.movie));

                break;

            case BusinessEmue.Shopping:

                holder.iv_rights_lei.setImageDrawable(mcontext.getResources().getDrawable(R.mipmap.goods));

                break;

            default:

                holder.iv_rights_lei.setImageDrawable(mcontext.getResources().getDrawable(R.mipmap.others));

                break;

        }





        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                    mOnItemClickListener.onClick(position);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    mOnItemClickListener.onLongClick(position);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return merList.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_rights_logo;
        ImageView iv_rights_lei;
        TextView tv_rights_title;
        TextView  tv_rights_data;
        TextView  tv_rights_huiyuan;
        TextView xiangqingtext;
        public ViewHolder(View view) {
            super(view);

            iv_rights_logo= (ImageView) view.findViewById(R.id.iv_rights_logo);
            iv_rights_lei= (ImageView) view.findViewById(R.id.iv_rights_lei);
            tv_rights_title= (TextView) view.findViewById(R.id.tv_rights_title);
            tv_rights_data= (TextView) view.findViewById(R.id.tv_rights_data);
            tv_rights_huiyuan= (TextView) view.findViewById(R.id.tv_rights_huiyuan);
            xiangqingtext= (TextView) view.findViewById(R.id.xiangqingtext);
        }
    }

    public interface OnItemClickListener {
        void onClick(int position);

        void onLongClick(int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

}
